// 简单的功能测试脚本
const WebSocketServer = require('./src/WebSocketServer');
const MessageHandler = require('./src/MessageHandler');
const KeyboardController = require('./src/KeyboardController');
const ScreenshotCapture = require('./src/ScreenshotCapture');

console.log('开始功能测试...\n');

// 测试1: WebSocket服务器
console.log('测试1: WebSocket服务器初始化');
// 创建一个模拟的HTTP服务器对象
const mockHttpServer = { server: null };
const wsServer = new WebSocketServer(mockHttpServer);
console.log('✓ WebSocket服务器创建成功\n');

// 测试2: 消息处理器
console.log('测试2: 消息处理器初始化');
const messageHandler = new MessageHandler(wsServer);
console.log('✓ 消息处理器创建成功\n');

// 测试3: 键盘控制器
console.log('测试3: 键盘控制器初始化');
const keyboardController = new KeyboardController();
console.log('✓ 键盘控制器创建成功\n');

// 测试4: 截图捕获
console.log('测试4: 截图捕获模块初始化');
const screenshotCapture = new ScreenshotCapture({
  quality: 75,
  maxWidth: 800
});
console.log('✓ 截图捕获模块创建成功\n');

// 测试5: 消息处理
console.log('测试5: 测试消息处理');
const testMessage = {
  type: 'control',
  action: 'next',
  timestamp: Date.now()
};

let controlReceived = false;
messageHandler.on('control', (action) => {
  console.log(`✓ 收到控制指令: ${action}`);
  controlReceived = true;
});

messageHandler.handleMessage(testMessage, {});
if (controlReceived) {
  console.log('✓ 消息处理测试通过\n');
} else {
  console.log('✗ 消息处理测试失败\n');
}

// 测试6: 性能测试
console.log('测试6: 性能测试');
const iterations = 100;
const startTime = Date.now();

for (let i = 0; i < iterations; i++) {
  messageHandler.handleMessage({
    type: 'control',
    action: i % 2 === 0 ? 'next' : 'previous',
    timestamp: Date.now()
  }, {});
}

const endTime = Date.now();
const avgTime = (endTime - startTime) / iterations;

console.log(`处理 ${iterations} 条消息`);
console.log(`总时间: ${endTime - startTime}ms`);
console.log(`平均时间: ${avgTime.toFixed(2)}ms`);

if (avgTime < 50) {
  console.log('✓ 性能测试通过 (< 50ms)\n');
} else {
  console.log(`✗ 性能测试失败 (${avgTime.toFixed(2)}ms > 50ms)\n`);
}

console.log('所有测试完成！');
process.exit(0);
